package com.shop.web.shiro.session;

import com.shop.web.context.WebContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

/**
 * 自定义 SessionManager，获取sessionId
 *
 * @return
 */
public class ShiroSessionManager extends DefaultWebSessionManager {

    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        // header 获取 - POST 请求
        String accessToken = WebContext.getAccessToken();
        if(StringUtils.isNotBlank(accessToken)) {
            return accessToken;
        }
        // parameter 获取 - GET 请求
        accessToken = request.getParameter("accessToken");
        if(StringUtils.isNotBlank(accessToken)) {
            return accessToken;
        }
        return null;
    }
}
